# -*- coding: UTF-8 -*-
# 安装python 
import pyautogui
import threading
import time
from cnocr import CnOcr
import queue
import signal
import numpy as np

# 单特质，刷到就不刷新了
refreshLable=['不灭','血疫幸存者','杀不死的人']
# 双特质，刷到两个就不刷新了
refreshLable2=['不灭','血疫幸存者','练瑜伽','从未哭泣','强壮',
'防守派','知路报童','杀不死的人','铁石心肠',
'硬汉','无敌之人','超强免疫系统','暗袋持有者',
'有韧性','练瑜伽','麻雀般的胃','脚步不停','睡在地上',
'从未生病','疼痛阀值很高','宽肩','地衣学家','社区守夜者',
'哪儿都能睡','决心之人','曾经的亿万富翁'
,'深蹲铁人','养蜂人','自信之人','通晓三语',
'年轻']
# refreshLable2=[]
# refreshLable3=['强壮']
pyautogui.FAILSAFE = True
lock=threading.RLock()
ocr =CnOcr()
# 每个 点击动作延迟0.1 秒
pyautogui.PAUSE = 0.1
q = queue.Queue(maxsize = 3) 
isExist=False

# 主方法 不断找特质和刷新
def dealOneChoosePerson(pos,choosehalfWidth,index,tezhiArr):
    im=pyautogui.screenshot(region=(pos.left,pos.top+30,200, 100))
    # 开始识别图片
    result = ocr.ocr(np.asarray(im))
    result=list(map(lambda x:x[0].strip('·').strip('。'),result))
    num=len(tezhiArr)
    print(result)
    # 特质不可能小于等于1 ,用来按下esc 可以停止程序
    if(num>1):
        try:
            haotezhi=2
            yigetezhi=1
            lock.acquire
            for tezhi in list(result):
                if tezhi in refreshLable2:
                    haotezhi=haotezhi-1
                    # num=0
                if tezhi in refreshLable:
                    yigetezhi=yigetezhi-1
            if(haotezhi==0 or yigetezhi==0):
                print(str(index)+'卡片刷新成功')
            else:
                #将位置加入队列列表
                q.put((pos,choosehalfWidth,index,tezhiArr))
        finally:
            lock.release

def sigint_handler(signum,frame):
    global isExist
    isExist=True
    print("stop!!")

if __name__=='__main__':
    # 移动到左上角 会停止程序
    print('hello,world')
    signal.signal(signal.SIGINT,sigint_handler)
    signal.signal(signal.SIGTERM,sigint_handler)
    refreshArr=[]
    leftVal=0
    indexy=0
    # 使用confidence 需要pytorch 会更精确
    for  pos in pyautogui.locateAllOnScreen('refresh.png',confidence=0.9,grayscale=True):
    # for  pos in pyautogui.locateAllOnScreen('refresh.png',grayscale=True):    
        if leftVal==0:
            refreshArr.insert(indexy,pos)
            leftVal=pos.left  
            indexy=indexy+1
        elif abs(refreshArr[indexy-1].left-pos.left)>20:
            refreshArr.insert(indexy,pos)
            indexy=indexy+1
            leftVal=pos.left
    print(refreshArr)        
    if len(refreshArr)!=3:
        print('需要把整个选人页面无遮挡显示在最上面')
    else:
        choosehalfWidth=(refreshArr[1].left-  refreshArr[0].left)/2
        print("看看第一个特质截图能不能全覆盖，看1.png")
        pyautogui.screenshot(imageFilename="1.png",region=(refreshArr[0].left,refreshArr[0].top+30,200, 100))
        dealOneChoosePerson(refreshArr[0],choosehalfWidth,0,refreshLable)     
        dealOneChoosePerson(refreshArr[1],choosehalfWidth,1,refreshLable)  
        dealOneChoosePerson(refreshArr[2],choosehalfWidth,2,refreshLable)  
    while  True:
        param=q.get(True,10)
        if isExist or  param==None:
            break;
        pyautogui.middleClick(param[0].left,param[0].top)
        pyautogui.press("T",1)
        t=threading.Thread(target=dealOneChoosePerson,args=(param[0],param[1],param[2],param[3]),daemon=True)
        t.start()
        


